﻿namespace Nello.Web.Controls
{
    using Nello;
    using Nello.Web;
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ViewStateModeById]
    public class NCommand : Label, IDataControl
    {
        private Nello.DataType datatype = Nello.DataType.String;
        private string ViewStateKeyAdd = "NCommand_IsAdd";
        private string ViewStateKeyEdit = "NCommand_IsEdit";
        private string ViewStateKeyView = "NCommand_IsView";

        public NCommand()
        {
            this.Visible = false;
        }

        [Category("Nello")]
        public Nello.DataType DataType
        {
            get
            {
                return this.datatype;
            }
            set
            {
                this.datatype = value;
            }
        }

        [DefaultValue(""), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public object DataValue
        {
            get
            {
                return DataTypeUtil.GetValue(this.datatype, this.Text);
            }
            set
            {
                if (value == null)
                {
                    this.Text = "";
                }
                else
                {
                    this.Text = value.ToString();
                }
            }
        }

        public string FormType
        {
            get
            {
                return this.Page.Request.QueryString["FormType"];
            }
        }

        [Browsable(false)]
        public bool IsAdd
        {
            get
            {
                object obj = this.ViewState[this.ViewStateKeyAdd];
                if (obj != null)
                {
                    return bool.Parse(obj.ToString());
                }
                return (this.Page.Request.QueryString["FormType"] == "Add");
            }
            set
            {
                this.ViewState[this.ViewStateKeyAdd] = value;
            }
        }

        [Browsable(false)]
        public bool IsEdit
        {
            get
            {
                object obj = this.ViewState[this.ViewStateKeyEdit];
                if (obj != null)
                {
                    return bool.Parse(obj.ToString());
                }
                return (this.Page.Request.QueryString["FormType"] == "Edit");
            }
            set
            {
                this.ViewState[this.ViewStateKeyEdit] = value;
            }
        }

        public bool IsIgnore
        {
            get
            {
                return false;
            }
            set
            {
            }
        }

        [Browsable(false)]
        public bool IsView
        {
            get
            {
                object obj = this.ViewState[this.ViewStateKeyView];
                if (obj != null)
                {
                    return bool.Parse(obj.ToString());
                }
                return (this.Page.Request.QueryString["FormType"] == "View");
            }
            set
            {
                this.ViewState[this.ViewStateKeyView] = value;
            }
        }
    }
}

